/*
 * commandqueue.cpp
 *
 *  Created on: Dec 20, 2009
 *      Author: kevin
 */

#include "commandqueue.h"
#include <iostream>

using namespace std;

CommandQueue::CommandQueue() :
	_lock(0,1000000)
{
	cout << "CommandQueue :: Constructing Commandqueue " << endl;
}

CommandQueue::~CommandQueue()
{
	cout << "CommandQueue :: Destroying Commandqueue " << endl;
}

void CommandQueue::push( Command* cmd )
{
	cout << "CommandQueue :: Pushing a command" << endl;
	cout << "    -> " << cmd->repr() << endl;
	commands.push(cmd);
	cout <<"CommandQueue :: Incrementing semaphore "<< endl;
	_lock.set();
}

Command* CommandQueue::pop()
{
	cout << "CommandQueue :: Waiting for a command to pop" << endl;
	_lock.wait();
	cout << "CommandQueue :: Popping a command " << endl;
	Command* cmd = commands.front();
	cout << "    -> " << cmd->repr() << endl;
	cout << "CommandQueue :: Command out of queue" << endl;
	commands.pop();
	cout << "ComandQueue :: Command Pooped " << endl;
	return ( cmd );
}

bool CommandQueue::isEmpty()
{
	return commands.empty();
}
